﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;

namespace MishraReader.ViewModels.Base
{
    public interface IDialogViewModel
    {
        bool? Result { get; }
        bool ShowData { get; set; }

        void ShowDialog();
    }

    public abstract class DialogViewModelBase : TaskViewModelBase, IDialogViewModel
    {
        private bool? _result;
        private bool _showData;

        protected DialogViewModelBase() 
        {
            CancelCommand = new RelayCommand(ExecuteCancelCommand);
            OkayCommand = new RelayCommand(ExecuteOkayCommand, CanExecuteOkay);
            ShowDialogCommand = new RelayCommand(ShowDialog);
        }

        public ICommand CancelCommand { get; private set; }
        public ICommand OkayCommand { get; private set; }

        public bool? Result
        {
            get { return _result; }
            protected set
            {
                Set(ref _result, value);
            }
        }

        public bool ShowData
        {
            get { return _showData; }
            set
            {
                var result = _result;
                _result = null;
                _showData = value;
                RaisePropertyChanged();

                if (!value)
                    OnDialogClosed(result);
            }
        }

        public ICommand ShowDialogCommand { get; private set; }

        public void ShowDialog()
        {
            ShowData = true;
        }

        protected virtual bool CanExecuteOkay()
        {
            return true;
        }

        protected void ExecuteCancelCommand()
        {
            OnCancelExecuted();

            Result = false;
            ShowData = false;
        }


        protected void ExecuteOkayCommand()
        {
            OnOkayExecuted();

            Result = true;
            ShowData = false;
        }

        protected virtual void OnCancelExecuted()
        {
        }

        protected virtual void OnDialogClosed(bool? result)
        {
        }

        protected virtual void OnOkayExecuted()
        {
        }
    }
}